import { Component, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute, Params } from '@angular/router';
import { HttpService } from 'src/app/service/http.service';
import { UrlService } from 'src/app/service/url.service';
import { ConfigService } from 'src/app/service/config.service';
import { ToolService } from 'src/app/service/tool.service';
import { PopwinService } from 'src/app/service/popwin.service';
import { NavController, IonInfiniteScroll, IonRefresher } from '@ionic/angular';

@Component({
  selector: 'app-transactionlist',
  templateUrl: './transactionlist.page.html',
  styleUrls: ['./transactionlist.page.scss'],
})
export class TransactionlistPage implements OnInit {
  private signId;
  private sTime;
  private eTime;
  public listData: any[] = [];
  private page = 1;
  @ViewChild(IonInfiniteScroll) infiniteScroll: IonInfiniteScroll;
  @ViewChild(IonRefresher) ionRefresher: IonRefresher;
  constructor(private activeRoute: ActivatedRoute, private http: HttpService, private popWin: PopwinService, private navCtrl: NavController) {
    this.activeRoute.queryParams.subscribe((params: Params) => {
      let tempDeal=JSON.parse(ToolService.paramAESDecrypt(params['p']));
      this.signId = tempDeal['signid'];
      this.sTime = tempDeal['stime'];
      this.eTime = tempDeal['etime'];
    });
  }

  ngOnInit() {
    if (ToolService.isEmpty(this.signId)) {
      this.navCtrl.navigateRoot("/transaction");
      return;
    }
    this.getTransactionlist();
  }

  backClick() {
    this.navCtrl.navigateRoot("/transaction");
  }

  getTransactionlist(event?: any, isload?: boolean) {
    let that = this;
    let obj = "{\"signid\":\"" + that.signId + "\",\"sdate\":\"" + that.sTime + "\",\"edate\":\"" + that.eTime + "\",\"page\":\"" + that.page + "\"}";
    console.log(obj);
    this.http.get(UrlService.MMNU_SHOWACCTLIST, { "callback": "JSONP_CALLBACK", "data": ToolService.aesEncode(obj) }).subscribe(res => {
      let data = res.json();
      ToolService.setEncrypt(data.encrypt);
      if (data.code == ConfigService.SUCCESS_CODE) {
        let temp = data.data;
        let res = ToolService.aesDecodeToJson(temp);
        if (isload) {
          if (res.rowset.length > 0) {
            that.listData = that.listData.concat(res.rowset);
          } else {
            that.infiniteScroll.disabled = true;
            that.popWin.toast("数据已经加载完成");
          }
        } else {
          that.listData = res.rowset;
        }
        that.ionRefresher.complete();
        if (event) {
          that.infiniteScroll.complete();
        }
      } else if (data.code == ConfigService.FAIL_CODE) {
        that.popWin.toast(data.message);
      } else if (data.code == ConfigService.LOGIN_CODE) {
        that.navCtrl.navigateForward("/login");
      }
    }, error => {
      console.log("error", error);
      that.ionRefresher.complete();
      if (event) {
        that.infiniteScroll.complete();
      }
    });
  }


  doRefresh(event) {
    this.page = 1;
    this.infiniteScroll.disabled = false;
    this.getTransactionlist();
  }

  loadData(event) {
    this.page++;
    this.getTransactionlist(event, true);
  }
}
